package cn.wen.yaling.xo.service.impl;

import cn.wen.yaling.commons.enums.ImageStatus;
import cn.wen.yaling.commons.utils.ResultEntity;
import cn.wen.yaling.commons.vo.ImageAddVO;
import cn.wen.yaling.commons.vo.ImageUpdateVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.wen.yaling.commons.utils.PageUtils;
import cn.wen.yaling.commons.utils.Query;

import cn.wen.yaling.xo.dao.ImageDao;
import cn.wen.yaling.commons.entity.ImageEntity;
import cn.wen.yaling.xo.service.ImageService;


@Service("imageService")
public class ImageServiceImpl extends ServiceImpl<ImageDao, ImageEntity> implements ImageService {

    @Autowired
    private ImageDao imageDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ImageEntity> page = this.page(
                new Query<ImageEntity>().getPage(params),
                new QueryWrapper<ImageEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<ImageEntity> getCommentAvatarList() {
        return imageDao.getCommentAvatarList();
    }

    @Override
    public ResultEntity getImageListByTypeId(Integer typeId) {
        QueryWrapper<ImageEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("image_status", ImageStatus.DELETED.getStatus());
        queryWrapper.eq("type_id", typeId);
        List<ImageEntity> imageEntities = imageDao.selectList(queryWrapper);
        return ResultEntity.ok().put("data", imageEntities);
    }

    @Override
    public ResultEntity addImage(ImageAddVO imageAddVO) {
        ImageEntity target = new ImageEntity();
        target.setCreateTime(new Date());
        target.setImageStatus(ImageStatus.NORMAL_USE.getStatus());
        target.setUpdateTime(new Date());
        boolean res = imageDao.insert(target) > 0;
        if (res) return ResultEntity.ok();
        return ResultEntity.error();
    }

    @Override
    public ResultEntity updateImage(ImageUpdateVO imageUpdateVO) {
        ImageEntity target = new ImageEntity();
        target.setUpdateTime(new Date());
        boolean res = imageDao.updateById(target) > 0;
        if (res) return ResultEntity.ok();
        return ResultEntity.error();
    }

    @Override
    public ResultEntity deleteBatchImageByIds(List<Integer> imageIds) {
        boolean res = imageDao.updateBatchImageByIds(imageIds, ImageStatus.DELETED.getStatus());
        if (res) return ResultEntity.ok();
        return ResultEntity.error();
    }
}